<?php

namespace co2\v0_0_0;

class String

{
	protected
		$string
		;
		
	public function
	__construct(
		$string=null
	)
	
	{
		if(is_string((string) $string)) $this->string=$string;
	}
	
	public function
	__toString()
	
	{
		return $this->string;
	}
	
	public function
	trim()
	{
		$this->string=trim($this->string);
		return $this;
	}
	
	public function
	match(
		$regex,
		$modifier=''
	)
	
	{
		return preg_match('/'.preg_replace('%/%','\/',$regex).'/'.$modifier,$this->string);
	}
	
	public function
	replace(
		$regex,
		$substitute,
		$modifier=''
	)
	
	{
		$this->string=preg_replace('/'.preg_replace('%/%','\/',$regex).'/'.$modifier,$substitute,$this->string);
		
		return $this;
	}
	
	public function
	toLines(
		$index=false
	)
	{
		$lines=preg_split('/\n/',$this->string);
		
		if(is_int($index))
		
		{
			if(isset($lines[$index]))
			return $lines[$index];
			
			new Exception('unknown line index: '.$index);
		}
		
		return $lines;
	}
}
